package org.ktjy.springbootsecurity.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private PasswordEncoder passwordEncoder; // 必须注入

    @Override
    public UserDetails loadUserByUsername(String username) {
        if (!"admin".equals(username)) {
            throw new UsernameNotFoundException("用户不存在");
        }
        return new User(
                username,
                passwordEncoder.encode("123"), // BCrypt加密密码
                AuthorityUtils.createAuthorityList(
                        "ROLE_admin","ROLE_normal","ROLE_abc") // 只给admin角色
        );
    }
}